package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.acticle.domain.StrategyContent;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo.acticle.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.service.IStrategyService;
import cn.wolfcode.luowowo.acticle.service.IStrategyThemeService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 攻略明细资源管理器
 */
@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;

    //大攻略明细列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo){
        PageInfo pageInfo = strategyDetailService .query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategyDetail/list";
    }
    
    //保存攻略明细
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, String tags){
        strategyDetailService.saveOrUpdate(strategyDetail, tags);
        return new AjaxResult();
    }
    //删除攻略明细
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        strategyDetailService.delete(id);
        return new AjaxResult();
    }
    //跳转到编辑页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        model.addAttribute("strategies", strategyService.listAll()); //共享攻略数据
        model.addAttribute("themes", strategyThemeService.listAll()); //共享攻略主题数据
        if(id != null){
            StrategyDetail detail = strategyDetailService.get(id);
            StrategyContent strategyContent = strategyDetailService.getContent(id); //查询攻略文章内容
            detail.setStrategyContent(strategyContent);
            model.addAttribute("strategyDetail", detail); //共享攻略主题数据
            model.addAttribute("tags", strategyDetailService.getTagsByDetailId(id)); //共享标签内容
        }
        return "strategyDetail/input";
    }

    //修改攻略明细状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Long id, Integer state){
        strategyDetailService.changeState(id, state);
        return new AjaxResult();
    }

    //根据攻略id获取攻略分类
    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId){
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

}
